//
//  TP.m
//  10-10
//
//  Created by 钟纪旺 on 16/11/28.
//  Copyright © 2016年 zjw. All rights reserved.
//

#import "TP.h"
#import "TPCell.h"
#import "ScrollView.h"
#import "lon.h"
#import "FiXiViewController.h"
@interface TP ()<
UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIImagePickerControllerDelegate,UINavigationControllerDelegate

>
{
    NSMutableArray<UIImage *> *data;
}
@property (strong, nonatomic) IBOutlet UICollectionView *collectonView;
@end

@implementation TP
- (IBAction)onClick:(id)sender {
    
    
    UIImagePickerController *con = [[UIImagePickerController alloc] init];
    con.delegate = self;
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选取图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        con.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:con animated:YES completion:nil];
        
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }]];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    [data addObject:image];
    [self.collectonView reloadData];
    NSLog(@"%@",info);
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    data = [NSMutableArray new];
    self.collectonView.backgroundColor = [UIColor whiteColor];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return data.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{
    TPCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"on" forIndexPath:indexPath];
    cell.image.image = data[indexPath.row];
    return cell;
    
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    ScrollView * con = [self.storyboard instantiateViewControllerWithIdentifier:@"scrool"];
//    con.images = data;
 
    
    lon *con = [[lon alloc] init];
    con.images = data;
    con.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
   
    
    [self presentViewController:con animated:YES completion:nil];
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((self.view.frame.size.width - 3 * 2)/3 , (self.view.frame.size.width - 3 * 2)/3);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 3;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 3;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
